package pimp.organizermanager;

import javax.swing.tree.*;

public class LabelTree {

    private PNode root;
    private PNode transactions;
    private PNode todos;
    private PNode events;

    public LabelTree(TreeModel model) {


        TreeNode rootFromModel = (TreeNode) model.getRoot();

        root = new PNode("root");

        transactions = new PNode("Transaction");
        todos = new PNode("ToDo");
        events = new PNode("Event");

        for(int i=0, n=rootFromModel.getChildCount(); i<n; i++)
        {
            transactions.addChild( createPNodeTree(rootFromModel.getChildAt(i)) );
            todos.addChild( createPNodeTree(rootFromModel.getChildAt(i)) );
            events.addChild( createPNodeTree(rootFromModel.getChildAt(i)) );
        }
        root.addChild(transactions);
        root.addChild(todos);
        root.addChild(events);

    }

    public boolean addGlobalEvent( GlobalEvent globalEvent) {

        String pnodeName, categoryName;
        PNode pnode = root;


        for(int i=0, n=globalEvent.getId().size(); i<n ; i++)
        {
            categoryName = globalEvent.getId().elementAt(i);

            for(int j=0, m=pnode.getChildCount(); j<m ; j++)
            {
                pnodeName = pnode.getChildAt(j).getName();
                
                if(pnodeName.equals(categoryName))
                {
                    pnode = (PNode) pnode.getChildAt(j);
                    break;
                }
            }
        }

        globalEvent.getId().setUniqueId(pnode.getUnusedUniqueID());

        return pnode.addGlobalEvent(globalEvent);

        
    }

    public TreeModel getLabelTreeModel() {
        return  new DefaultTreeModel(root.transformToNode());
    }


    /*  używane do inicjalizacji drzewa odpowiednimi kategoriami zaczerpniętymi z GUI
     */
    private PNode createPNodeTree(TreeNode node) {

        PNode newPNode = new PNode(node.toString());
        System.out.println(node);

        int n = node.getChildCount();
        for(int i=0; i<n; i++)
        {
            newPNode.addChild(createPNodeTree(node.getChildAt(i)));
        }

        return newPNode;
    }


    public int getNewSpecialIdFromPath(String path)
    {


        return 0;
    }

}
